package fr.alma.ihm;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import fr.alma.coeur.GestionPlugins;

/**
 * Classe de configuration des plugins.
 * @author AUBERT Sebastien
 * @author BOUVET Frederic
 * @author BRAUD Jeremy
 * @author KROMMENHOEK Cedric
 *
 */
public final class ConfigurationDialog extends JDialog {

	/**
	 * Generated serial ID.
	 */
	private static final long serialVersionUID = -5046982080779666105L;
	
	/**
	 * Classe des gestion des plugins.
	 */
	private GestionPlugins gestionPlugins;
	/**
	 * Liste des plugins selectionnes.
	 */
	private ArrayList<String> pluginsSelectionnes;
	
	/**
	 * Constructeur.
	 * @param parent la fenetre appelante
	 * @param title le titre de la fenetre
	 * @param modal si la fenetre est modale
	 * @param gestion la classe de gestion des plugins
	 */
	public ConfigurationDialog(JFrame parent, String title, boolean modal, GestionPlugins gestion) {
		super(parent, title, modal);
		this.gestionPlugins = gestion;
		this.pluginsSelectionnes = new ArrayList<String>();
		this.setSize(250, 250);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.initComponent();
	}

	/**
	 * Initialise la fenetre.
	 */
	private void initComponent() {
		JPanel panPlugins = new JPanel();
		panPlugins.setLayout(new BoxLayout(panPlugins, BoxLayout.Y_AXIS));
		ArrayList<String> pluginEntree = this.gestionPlugins.listePluginsEntree();
		for (final String plugin : pluginEntree) {
			JPanel pan = new JPanel();
			//pan.setLayout(new GridLayout(1,2));
			//pan.setAlignmentX(LEFT_ALIGNMENT);
			JCheckBox activerPlugin = new JCheckBox(plugin);
			final JButton configurerPlugin = new JButton("Configurer");
			boolean bool = this.gestionPlugins.estPluginEntreeSelectionne(plugin);
			activerPlugin.setSelected(bool);
			configurerPlugin.setEnabled(bool);
			if (bool) {
				this.pluginsSelectionnes.add(plugin);
			}
			activerPlugin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (((AbstractButton) arg0.getSource()).isSelected()) {
						configurerPlugin.setEnabled(true);
						pluginsSelectionnes.add(plugin);
					} else {
						configurerPlugin.setEnabled(false);
						pluginsSelectionnes.remove(plugin);
					}
				}
			});
			configurerPlugin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String t = "Configuration du plugin " + plugin;
					new ConfigurationPlugin(null, t, true, plugin, gestionPlugins).setVisible(true);
				}
			});
			//ajouter tt ca sur la fenetre
			pan.add(activerPlugin);
			pan.add(configurerPlugin);
			panPlugins.add(pan);
		}
		JScrollPane scroll = new JScrollPane(panPlugins);
		//scroll.setLayout(new ScrollPaneLayout());
		
		
		JPanel control = new JPanel();
		JButton okBouton = new JButton("OK");
		okBouton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gestionPlugins.setPluginsSelectionnes(pluginsSelectionnes);
				setVisible(false);
			}
		});
		JButton cancelBouton = new JButton("Annuler");
		cancelBouton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}			
		});
		control.add(okBouton);
		control.add(cancelBouton);
		
		this.getContentPane().add(scroll, BorderLayout.CENTER);
		this.getContentPane().add(control, BorderLayout.SOUTH);
	}

}
